package com.jiutong.controller; /**
 * Created by sty on 2017/2/21.
 */

import com.jiutong.dao.ProductDao;
import com.jiutong.dao.UserDao;
import com.jiutong.dao.jiutong.ProductMapper;
import com.jiutong.dao.jiutongUser.UserMapper;
import com.jiutong.pojo.Product;
import com.jiutong.pojo.User;
import com.jiutong.service.ProductService;
import com.jiutong.service.UserService;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Controller
public class SampleController {

//    @Resource
//    private ProductDao productDao;

    @Resource
    private ProductMapper productMapper;

    @Resource
    private UserMapper userMapper;

//    @Resource
//    private UserDao userDao;
    @Resource
    private ProductService productService;

    @Resource
    private UserService userService;

    private static Logger logger = Logger.getLogger(SampleController.class);

    @RequestMapping("/")
    @ResponseBody
    String home() {

        System.out.println("System.out.println hello");//该输出不会打印到log4j日志文件中
        logger.info("info日志打印");//会打印到配置的warn文件
        logger.warn("warn日志打印");//会打印到配置的error文件
        try{
            Integer.parseInt("a");
        }catch (Exception e){
            e.printStackTrace();
            logger.error("出错了",e);
        }

        return "Hello World!";


    }

    @RequestMapping("/findProductById")
    @ResponseBody
    JSONObject findProductById(){
        return JSONObject.fromObject(productMapper.findById(1));
    }

    @RequestMapping("/findUserById")
    @ResponseBody
    JSONObject findUserById(){
        return JSONObject.fromObject(userMapper.findById(1));
    }

    @RequestMapping("/saveProduct")
    @ResponseBody
    JSONObject saveProduct(Product product){
        JSONObject result = new JSONObject();
        result.put("update",productService.saveProduct(product));
        return result;
    }
    @RequestMapping("/saveUser")
    @ResponseBody
    JSONObject saveUser(User user){
        JSONObject result = new JSONObject();
        result.put("update",userService.saveUser(user));
        return result;
    }

}